require('lodash')
// 首页s
export function chanyejiegoufenxi(id, data) {
  var map = {};
  var sum = 0
  data.map(function (obj) {
    sum+=obj.value
  });
  data.map(function (obj) {
    map[obj.name] = Math.round(obj.value/sum*100) + "%";
  });

  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio,
    padding: [-100, "auto", 20],
  });
  chart.source(data, {
    value: {
      formatter: function formatter(val) {
        return val + "%";
      },
    },
  });
  chart.tooltip(false);
  chart.legend({
    position: "bottom",
    itemFormatter: function itemFormatter(val) {
      return val + "    " + map[val];
    },
  });
  chart.coord("polar", {
    transposed: true,
    innerRadius: 0.7,
    radius: 0.85,
  });
  chart.axis(false);
  chart
    .interval()
    .position("a*value")
    .color("name", ["#13c2c2", "#3BA4FF", "#737DDE", "#7ecf51", "#99cccc", "#ff6666", "#e3935d", "#ffe793","#CC0066"])
    .adjust("stack");

  chart.guide().html({
    position: ["50%", "50%"],
    html:
      '<div style="width: 250px;height: 40px;text-align: center;">' +
      '<div style="font-size: 16px">企业总数</div>' +
      '<div style="font-size: 24px">'+sum+'家</div>' +
      "</div>",
  });
  chart.render();
}
// 辖区内新企注册走势分析
export function zhucezoushifenxi(id, data) {
  var Global = F2.Global;
  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio
  });

  chart.source(data, {
    population: {
      tickCount: 5
    }
  });
  chart.coord({
    transposed: true
  });
  chart.axis('year', {
    line: Global._defaultAxis.line,
    grid: null
  });
  chart.axis('新注册企业数', {
    line: null,
    grid: Global._defaultAxis.grid,
    label: function label(text, index, total) {
      var textCfg = {};
      if (index === 0) {
        textCfg.textAlign = 'left';
      } else if (index === total - 1) {
        textCfg.textAlign = 'right';
      }
      return textCfg;
    }
  });
  chart.interval().position('year*新注册企业数').color('l(180) 0:#1890ff 1:#70cdd0');
  chart.render();
}
// 纳税走势
export function nashuizoushi(id, data) {
  var Global = F2.Global;
  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio
  });

  chart.source(data.reverse(), {
    // value: {
    //   tickInterval: 0
    // }
  });
  chart.coord({
    transposed: true
  });

  chart.tooltip({
    custom: true, // 自定义 tooltip 内容框
    onChange: function onChange(obj) {
      var legend = chart.get('legendController').legends.top[0];
      var tooltipItems = obj.items;
      var legendItems = legend.items;
      var map = {};
      legendItems.map(function (item) {
        map[item.name] = _.clone(item);
      });
      tooltipItems.map(function (item) {
        var name = item.name;
        var value = item.value;
        if (map[name]) {
          map[name].value = (value/100000000).toFixed(2)+'亿';
        }
      });
      legend.setItems(_.values(map));
    },
    onHide: function onHide() {
      var legend = chart.get('legendController').legends.top[0];
      legend.setItems(chart.getLegendItems().country);
    }
  });
  chart.axis('label', {
    line: F2.Global._defaultAxis.line,
    grid: null
  });
  chart.axis('value', {
    line: null,
    grid: F2.Global._defaultAxis.grid,
    label: function label(text, index, total) {
      var textCfg = {};
      textCfg.text = text/100000000+'亿'
      if (index === 0) {
        textCfg.textAlign = 'left';
      } else if (index === total - 1) {
        textCfg.textAlign = 'right';
      }
      return textCfg;
    }
  });
  chart.interval().position('label*value').color('type').adjust({
    type: 'dodge',
    marginRatio: 1 / 32
  });
  chart.render();
}
// 各月预警数量走势
export function yujingshuliang(id, data) {
  var Shape = F2.Shape;
  var Util = F2.Util;
  Shape.registerShape('interval', 'text', {
    draw: function draw(cfg, container) {
      var points = this.parsePoints(cfg.points);
      // points 顶点的顺序
      // 1 ---- 2
      // |      |
      // 0 ---- 3
      var style = Util.mix({
        fill: cfg.color,
        z: true // 需要闭合
      }, cfg.style);
      var intervalShape = container.addShape('rect', {
        attrs: Util.mix({
          x: points[1].x,
          y: points[1].y,
          width: points[2].x - points[1].x,
          height: points[0].y - points[1].y
        }, style)
      });

      var origin = cfg.origin._origin; // 获取对应的原始数据记录
      var textShape = container.addShape('text', {
        zIndex: 1,
        attrs: {
          x: (points[1].x + points[2].x) / 2,
          y: points[1].y - 5, // 往上偏移 5 像素
          text: origin['预警企业数'],
          fill: '#808080',
          textAlign: 'center',
          textBaseline: 'bottom',
          fontSize: 10 // 字体大小
        }
      });
      container.sort();
      return [intervalShape, textShape];
    }
  });
  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio
  });
  chart.source(data, {
    '月份': {
      tickCount: 12
    }
  });
  chart.legend(false);

  chart.interval().position('月份*预警企业数').color('l(90) 0:#f64b70 1:#fdc6a3').shape('text').adjust({
    type: 'dodge',
    marginRatio: 0.2
  }).style({
    radius: [4, 4, 0, 0]
  });

  chart.render();
}
// 首页e

// 经济指标s
export function jiedaoshuishouhuanbi(id, data) {
  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio
  });
  chart.source(data, {
    // time: {
    //   type: 'timeCat',
    //   tickCount: 12,
    //   mask: 'MM',
    //   range: [0, 1]
    // },
    // time: data.time,
    time: {
      tickCount: 12,
    },
    value: {
      tickCount: 3,
      formatter: function formatter(ivalue) {
        return ivalue + '%';
      }
    }
  });
  chart.axis('time', {
    line: null,
    label: function label(text, index, total) {
      var textCfg = {};
      if (index === 0) {
        textCfg.textAlign = 'left';
      } else if (index === total - 1) {
        textCfg.textAlign = 'right';
      }
      return textCfg;
    }
  });
  chart.axis('tem', {
    grid: function grid(text) {
      if (text === '0%') {
        return {
          lineDash: null,
          lineWidth: 1
        };
      }
    }
  });
  chart.legend({
    position: 'bottom',
    offsetY: -5
  });
  chart.line().position('time*value').color('type').shape('type', function (type) {
    if (type === '2020年') {
      return 'line';
    }
    if (type === '2019年') {
      return 'dash';
    }
  });

  chart.render();
}
export function jiedaoshuishouzoushi(id, data,count,title) {
  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio
  });

  chart.source(data);
  chart.tooltip({
    showCrosshairs: true
  });
  chart.scale({
    time: {
      tickCount: count,
      range: [0, 1]
    },
    title: {
      tickCount: 5,
      min: 0
    }
  });
  chart.axis('time', {
    label: function label(text, index, total) {
      var textCfg = {};
      if (index === 0) {
        textCfg.textAlign = 'left';
      } else if (index === total - 1) {
        textCfg.textAlign = 'right';
      }
      return textCfg;
    }
  });
  chart.area().position('time*'+title);
  chart.line().position('time*'+title);
  chart.render();
}
export function chanyeshuishougongxian(id, data) {
  var map = {};
  var sum = 0
  data.map(function (obj) {
    sum+=obj.value
  });
  data.map(function (obj) {
    map[obj.name] = Math.round(obj.value/sum*100) + "%";
  });

  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio,
    padding: [-100, "auto", 20],
  });
  chart.source(data, {
    value: {
      formatter: function formatter(val) {
        return val + "%";
      },
    },
  });
  chart.tooltip(false);
  chart.legend({
    position: "bottom",
    itemFormatter: function itemFormatter(val) {
      return val + "    " + map[val];
    },
  });
  chart.coord("polar", {
    transposed: true,
    innerRadius: 0.7,
    radius: 0.85,
  });
  chart.axis(false);
  chart
    .interval()
    .position("a*value")
    .color("name", ["#13c2c2", "#3BA4FF", "#737DDE", "#7ecf51", "#99cccc", "#ff6666", "#e3935d", "#ffe793"])
    .adjust("stack");

  chart.guide().html({
    position: ["50%", "50%"],
    html:
      '<div style="width: 250px;height: 40px;text-align: center;">' +
      '<div style="font-size: 16px">企业税收总数</div>' +
      '<div style="font-size: 24px">'+sum+'亿</div>' +
      "</div>",
  });
  chart.render();
}
// 本年度企业纳税分析
export function qiyenashuifenxi(id, data, map) {
  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio
  });
  chart.source(data, {
    percent: {
      formatter: function formatter(val) {
        return val * 100 + '%';
      }
    }
  });
  chart.legend({
    position: 'bottom',
    itemFormatter: function itemFormatter(val) {
      return val + '  ' + map[val];
    }
  });
  chart.tooltip(false);
  chart.coord('polar', {
    transposed: true,
    radius: 0.85
  });
  chart.axis(false);
  chart.interval().position('a*percent').color('name', ['#1890FF', '#13C2C2', '#2FC25B', '#FACC14', '#F04864', '#8543E0']).adjust('stack').style({
    lineWidth: 1,
    stroke: '#fff',
    lineJoin: 'round',
    lineCap: 'round'
  }).animate({
    appear: {
      duration: 1200,
      easing: 'bounceOut'
    }
  });

  chart.render();
}
// 本年度园区税收分析
export function yuanqushuishoufenxi(id, data) {
  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio,
    padding:[50,'auto',30]
  });
  chart.source(data);
  chart.axis(false);
  chart.legend({
    position: "bottom",
    clickable: false,
    itemFormatter: function itemFormatter(val) {
      return val;
    },
  });
  // 指定初始和结束角度
  chart.coord('polar', {
    transposed: true,
    radiug: 0.9,
    innerRadius: 0.5,
    startAngle: 5 * Math.PI / 6,
    endAngle: 13 * Math.PI / 6
  });

  chart.guide().text({
    position: ['50%', '50%'],
    content: '本年度园区\n税收分析',
    style: {
      fontSize: 18,
      textAlign: 'center',
      textBaseLine: 'middle'
    }
  });

  chart.interval().position('const*seats').color('Group').adjust('stack').style({
    lineWidth: 1,
    stroke: '#fff'
  });

  chart.render();
}
// 经济指标e
// 企业走访s
export function zoufangjihua(id, data) {
  var Global = F2.Global;
  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio
  });

  chart.source(data);
  chart.coord({
    transposed: true
  });
  chart.axis('country', {
    line: Global._defaultAxis.line,
    grid: null
  });
  chart.axis('走访数量', {
    line: null,
    grid: F2.Global._defaultAxis.grid,
    label: function label(text, index, total) {
      var textCfg = {
        text:text
      };
      if (index === 0) {
        textCfg.textAlign = 'left';
      } else if (index === total - 1) {
        textCfg.textAlign = 'right';
      }
      return textCfg;
    }
  });
  chart.interval().position('country*走访数量');
  chart.render();
}
export function yichangpaicha(id, data) {
  var chart = new F2.Chart({
    id,
    pixelRatio: window.devicePixelRatio,
  });

  chart.source(data, {
    '问题数量': {
      tickCount: 5
    }
  });
  chart.coord({
    transposed: true
  });
  chart.axis('State', {
    line: F2.Global._defaultAxis.line,
    grid: null
  });
  chart.axis('问题数量', {
    line: null,
    grid: F2.Global._defaultAxis.grid,
    label: function label(text, index, total) {
      var textCfg = {
        text: text 
      };
      if (index === 0) {
        textCfg.textAlign = 'left';
      } else if (index === total - 1) {
        textCfg.textAlign = 'right';
      }
      return textCfg;
    }
  });
  chart.tooltip({
    custom: true, // 自定义 tooltip 内容框
    onChange: function onChange(obj) {
      var legend = chart.get('legendController').legends.top[0];
      var tooltipItems = obj.items;
      var legendItems = legend.items;
      var map = {};
      legendItems.map(function(item) {
        map[item.name] = _.clone(item);
      });
      tooltipItems.map(function(item) {
        var name = item.name;
        var value = item.value;
        if (map[name]) {
          map[name].value = value;
        }
      });
      legend.setItems(_.values(map));
    }
  });
  chart.interval().position('State*问题数量').color('类别').adjust('stack');

  chart.render();
}
export function wentichuzhilv(id,chartData) {
  var Shape = F2.Shape;
  var data = [
    {
      pointer: "问题处置率",
      value: chartData,
      length: 2,
      y: 1.05,
    },
  ];
  //自定义绘制数据的的形状
  Shape.registerShape("point", "dashBoard", {
    getPoints: function getPoints(cfg) {
      var x = cfg.x;
      var y = cfg.y;

      return [
        {
          x: x,
          y: y,
        },
        {
          x: x,
          y: 0.4,
        },
        {
          x: x,
          y: 0.95,
        },
        {
          x: x,
          y: 0.6,
        },
      ];
    },
    draw: function draw(cfg, container) {
      var point1 = cfg.points[2];
      var point2 = cfg.points[3];
      point1 = this.parsePoint(point1);
      point2 = this.parsePoint(point2);
      console.log("points", point1, point2, cfg);
      var line = container.addShape("Polyline", {
        attrs: {
          points: [point1, point2],
          stroke: "#1890FF",
          lineWidth: 3,
        },
      });

      var text = cfg.origin._origin.value.toString();
      var text1 = container.addShape("Text", {
        attrs: {
          text: text + "%",
          x: cfg.center.x,
          y: cfg.center.y + 10,
          fill: "#e16757",
          fontSize: 24,
          textAlign: "center",
          textBaseline: "bottom",
        },
      });
      var text2 = container.addShape("Text", {
        attrs: {
          text: cfg.origin._origin.pointer,
          x: cfg.center.x,
          y: cfg.center.y + 10,
          fillStyle: "#ccc",
          textAlign: "center",
          textBaseline: "top",
        },
      });

      return [line, text1, text2];
    },
  });

  var chart = new F2.Chart({
    id,
    animate: false,
    pixelRatio: window.devicePixelRatio,
    padding:['50']
  });
  chart.source(data, {
    value: {
      type: "linear",
      min: 0,
      max: 100,
      ticks: [0, 25, 50, 75, 100],
      nice: false,
    },
    length: {
      type: "linear",
      min: 0,
      max: 10,
    },
    y: {
      type: "linear",
      min: 0,
      max: 1,
    },
  });

  chart.coord("polar", {
    inner: 0,
    startAngle: -1.25 * Math.PI,
    endAngle: 0.25 * Math.PI,
  });

  //配置value轴刻度线
  chart.axis("value", {
    tickLine: {
      strokeStyle: "#b0d2f4",
      lineWidth: 2,
      length: -5,
    },
    label: null,
    grid: null,
    line: null,
  });

  chart.axis("y", false);

  //绘制仪表盘辅助元素
  chart.guide().arc({
    start: [0, 1.05],
    end: [24, 1.05],
    style: {
      strokeStyle: "#ccc",
      lineWidth: 5,
      lineCap: "round",
    },
  });
  chart.guide().arc({
    start: [26, 1.05],
    end: [49, 1.05],
    style: {
      strokeStyle: "#ccc",
      lineWidth: 5,
      lineCap: "round",
    },
  });
  chart.guide().arc({
    start: [51, 1.05],
    end: [74, 1.05],
    style: {
      strokeStyle: "#ccc",
      lineWidth: 5,
      lineCap: "round",
    },
  });
  chart.guide().arc({
    start: [76, 1.05],
    end: [100, 1.05],
    style: {
      strokeStyle: "#ccc",
      lineWidth: 5,
      lineCap: "round",
    },
  });
  chart.guide().arc({
    start: [0, 1.2],
    end: [100, 1.2],
    style: {
      strokeStyle: "#ccc",
      lineWidth: 1,
    },
  });
  if (data[0].value <= 25) {
    chart.guide().arc({
      start: [0, 1.05],
      end: [data[0].value, 1.05],
      style: {
        strokeStyle: "#1890ff",
        lineWidth: 5,
        lineCap: "round",
      },
    });
  } else if (data[0].value > 25 && data[0].value <= 50) {
    chart.guide().arc({
      start: [0, 1.05],
      end: [24, 1.05],
      style: {
        strokeStyle: "#1890ff",
        lineWidth: 5,
        lineCap: "round",
      },
    });
    chart.guide().arc({
      start: [26, 1.05],
      end: [data[0].value, 1.05],
      style: {
        strokeStyle: "#1890ff",
        lineWidth: 5,
        lineCap: "round",
      },
    });
  } else if (data[0].value > 51 && data[0].value <= 75) {
    chart.guide().arc({
      start: [0, 1.05],
      end: [24, 1.05],
      style: {
        strokeStyle: "#1890ff",
        lineWidth: 5,
        lineCap: "round",
      },
    });
    chart.guide().arc({
      start: [26, 1.05],
      end: [49, 1.05],
      style: {
        strokeStyle: "#1890ff",
        lineWidth: 5,
        lineCap: "round",
      },
    });
    chart.guide().arc({
      start: [51, 1.05],
      end: [data[0].value, 1.05],
      style: {
        strokeStyle: "#1890ff",
        lineWidth: 5,
        lineCap: "round",
      },
    });
  } else if (data[0].value > 76 && data[0].value <= 100) {
    chart.guide().arc({
      start: [0, 1.05],
      end: [24, 1.05],
      style: {
        strokeStyle: "#1890ff",
        lineWidth: 5,
        lineCap: "round",
      },
    });
    chart.guide().arc({
      start: [26, 1.05],
      end: [49, 1.05],
      style: {
        strokeStyle: "#1890ff",
        lineWidth: 5,
        lineCap: "round",
      },
    });
    chart.guide().arc({
      start: [51, 1.05],
      end: [74, 1.05],
      style: {
        strokeStyle: "#1890ff",
        lineWidth: 5,
        lineCap: "round",
      },
    });
    chart.guide().arc({
      start: [76, 1.05],
      end: [data[0].value, 1.05],
      style: {
        strokeStyle: "#1890ff",
        lineWidth: 5,
        lineCap: "round",
      },
    });
  }
  chart.guide().text({
    position: [-0.5, 1.3],
    content: "0.00%",
    style: {
      fillStyle: "#ccc",
      font: "18px Arial",
      textAlign: "center",
    },
  });
  chart.guide().text({
    position: [50, 0.7],
    content: "50.00%",
    style: {
      fillStyle: "#ccc",
      font: "18px Arial",
      textAlign: "center",
    },
  });
  chart.guide().text({
    position: [100, 1.3],
    content: "100.00%",
    style: {
      fillStyle: "#ccc",
      font: "18px Arial",
      textAlign: "center",
    },
  });

  chart
    .point()
    .position("value*y")
    .size("length")
    .color("#1890FF")
    .shape("dashBoard");
  chart.render();
}
// 企业走访e